#------------------------------------------#
# !python3
# coding:utf-8
# Author: Terry
# Descpiton: 利用requests 和lxml 模块爬取儿歌标题,并写入文件
#------------------------------------------#

import requests
import re
from lxml.html import fromstring
import json

base_url="http://www.9ku.com/erge/ertonggequ.htm"

# user-agent 列表Chrome/360/IE edge
agent1= "Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"
agent2="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 QIHU 360SE"
agent3="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"
header={
   "User-Agent":agent3,
   "Referer":"http://www.9ku.com/music/",
   "Accept-Encoding": "gzip, deflate",
   "Host":"www.9ku.com",
   "Cookie":"BAIDU_SSP_lcr=https://www.baidu.com/link?url=alfxVIBOlqgYeccZK1qTlGc0pd_B10oDdJoqjXgs3FhA9Uu2qHLWb9aA_rk1wVtE&wd=&eqid=da172ed00003cd9f000000065e257312; Hm_lvt_a5de315acb973b8e6da83458c9e456d3=1579512600; tt=ok; cc=ok; tmp_addplay=; pp=ok; shows=no; ff=no; Hm_lpvt_a5de315acb973b8e6da83458c9e456d3=1579513212; l_music=542053/67187/532733l_end; jk_addplay=542053/67187/532733/; jk_ifplay=0"
       }

page_target=base_url
prefix="9ku.com"
r=requests.get(base_url,headers=header)
data=fromstring(r.text)
selector=data.xpath('//div[@id="fall"]/div[@class="songList songList960 clearfix"]/ol[@id="f1"]/li/a')

# 直接定位，并获取属性值
# s2=data.xpath('//*[@id="f1"]/li[1]/a/font/text()')
# print (s2)


music_data=[]
for i in range(len(selector)):
    # 需要依据歌曲出现的序号来获取名称
    if (i+1)%5==1:
        title=selector[i].xpath('./font/text()')  #//*[@id="f1"]/li[1]/a/font
        addr=selector[i].xpath('@href')[0]
    else:
        title=selector[i].xpath('text()')       #//*[@id="f1"]/li[2]/a
        addr=selector[i].xpath('@href')[0]
    print(title,addr)
    pass
    music_data.append({"title":title if title else "",
                        "addr":(prefix+addr)
    })  

with open("f://music.json",'w+')as f:
    json.dump(music_data,f,indent=1,ensure_ascii=False)
    


#每5首歌第一首笔加一个<font>标签